package com.my.xinyidaixi.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.my.xinyidaixi.DataBase.Bean.Address;
import com.my.xinyidaixi.DataBase.Bean.Dingdan_unpaid_data;
import com.my.xinyidaixi.DataBase.Bean.Youhuijuan;
import com.my.xinyidaixi.DataBase.Model.Over_UserModer;
import com.my.xinyidaixi.DataBase.Model.UserModel;
import com.my.xinyidaixi.HttpVolley.Volley_core;
import com.my.xinyidaixi.Model.Daixiitem;
import com.my.xinyidaixi.Model.GoodsItem;
import com.my.xinyidaixi.R;
import com.my.xinyidaixi.Util.Dateutil.DateUtils;
import com.my.xinyidaixi.Util.ToastUtil;
import com.my.xinyidaixi.Util.wheelview_util.DoublePicker;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by zw on 2017/8/7.
 */
public class tijiao_Order extends Activity {

    private ImageView tijiao_order_fanhui;
    private ArrayList<GoodsItem> arraylist;
    private ArrayList<Daixiitem> daixilist;
    private RelativeLayout add_address,addtime,addyouhuijuan;
    private TextView addtime_text,tijiao_button,name,sex,tel,address, address_hint,youhuijuan,price,yunfei,zongjia,beizhu;
    private String yiwu="",School="",Address="";
    private static int cost;
    private static int zongjia_price;
    private static String coupon_id;
    private String clean_type;
    private Volley_core volley=new Volley_core();
    private UserModel model=new Over_UserModer();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tijiao_order);
        initdata();
    }

    public void initdata()
    {
        tijiao_order_fanhui = (ImageView) findViewById(R.id.tijiao_order_fanhui);
        tijiao_order_fanhui.setOnClickListener(new Button());

        tijiao_button = (TextView) findViewById(R.id.tijiao_Button);
        tijiao_button.setOnClickListener(new Button());

        add_address = (RelativeLayout) findViewById(R.id.addaddress);
        add_address.setOnClickListener(new Button());

        address_hint = (TextView) findViewById(R.id.tijiao_order_address_hint);
        name =  (TextView) findViewById(R.id.tijiao_order_name);
        sex = (TextView) findViewById(R.id.tijiao_order_sex);
        tel = (TextView) findViewById(R.id.tijiao_order_tel);
        address = (TextView) findViewById(R.id.tijiao_order_address);
        youhuijuan = (TextView) findViewById(R.id.tijiao_order_youhuijuan);

        addtime = (RelativeLayout) findViewById(R.id.addtime);
        addtime.setOnClickListener(new Button());
        addyouhuijuan = (RelativeLayout) findViewById(R.id.addyouhuijuan);
        addyouhuijuan.setOnClickListener(new Button());

        addtime_text = (TextView) findViewById(R.id.addtime_text);

        price = (TextView) findViewById(R.id.tijiao_order_price);
        yunfei = (TextView) findViewById(R.id.tijiao_order_yunfei);
        zongjia = (TextView) findViewById(R.id.tijiao_order_zongjia);
        beizhu = (TextView) findViewById(R.id.tijiao_order_beizhu);

        Intent intent=getIntent();
        arraylist= (ArrayList<GoodsItem>) intent.getSerializableExtra("arraylist");
        if(arraylist!=null)
        {
            cost = intent.getIntExtra("cost",0);
            price.setText(String.valueOf(cost)+"元");
            yunfei.setText("10元");//运费写死！！！！！

            zongjia_price=cost+10;
            zongjia.setText(String.valueOf(zongjia_price)+"元");
            //Toast.makeText(this,"arraylist大小为"+cost,Toast.LENGTH_SHORT).show();

            for (int i = 0; i < arraylist.size(); i++) {
                GoodsItem item= (GoodsItem)arraylist.get(i);
                yiwu=yiwu+item.name+"选了"+item.count+"件 ";
            }
            clean_type="件洗";
        }
        daixilist= (ArrayList<Daixiitem>) intent.getSerializableExtra("daixi");
        if(daixilist!=null)
        {
            Daixiitem item=daixilist.get(0);
            cost=Integer.parseInt(item.price);
            price.setText(item.price+"元");
            yunfei.setText("10元");
            zongjia_price=cost+10;
            zongjia.setText(String.valueOf(zongjia_price)+"元");
            yiwu=item.name+"选择"+item.count+"件";
            clean_type="袋洗";
        }
    }

    public  class Button implements View.OnClickListener
    {

        private Intent intent;

        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {

                case R.id.tijiao_order_fanhui:
                    tijiao_Order.this.finish();
                    overridePendingTransition(0,R.anim.ani_left_sign_out);//窗口向左滑出
                    break;

                case R.id.tijiao_Button:
                    if(TextUtils.isEmpty(name.getText().toString()))
                    {
                        Toast.makeText(tijiao_Order.this,"地址不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(TextUtils.isEmpty(addtime_text.getText().toString()))
                    {
                        Toast.makeText(tijiao_Order.this,"取件时间不能为空",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Dingdan_unpaid_data unpaid_data=new Dingdan_unpaid_data(model.getUser(), name.getText().toString(), sex.getText().toString(), tel.getText().toString(), School, Address, addtime_text.getText().toString(), yiwu, beizhu.getText().toString(), clean_type, "-1", "0", "0", String.valueOf(cost+10), "安卓");

                    volley.unpaid_order(unpaid_data);
                    volley.unpaid_order_callback(new Volley_core.unpaid_order_Callback() {
                        @Override
                        public void callback_data(JSONObject json) {

                            try {
                                String [] data_ordertime=new String[2];
                                data_ordertime[0]=json.getString("date");
                                data_ordertime[1]=json.getString("order_time");

                                intent = new Intent();
                                intent.putExtra("School",School);
                                intent.putExtra("address_info",Address);
                                intent.putExtra("name",name.getText().toString());
                                intent.putExtra("sex",sex.getText().toString());
                                intent.putExtra("tel",tel.getText().toString());
                                intent.putExtra("address",address.getText().toString());
                                intent.putExtra("time",addtime_text.getText().toString());
                                intent.putExtra("youhuijuan",youhuijuan.getText().toString());
                                intent.putExtra("youhuijuan_id",coupon_id);
                                intent.putExtra("price",String.valueOf(cost));
                                intent.putExtra("zongjia",String.valueOf(zongjia_price)); //运费写死了！！！！
                                intent.putExtra("beizhu",beizhu.getText().toString());
                                intent.putExtra("yiwu",yiwu);
                                intent.putExtra("clean_type",clean_type);
                                intent.putExtra("dingdanhao",data_ordertime[0]);
                                intent.putExtra("dingdanshijian",data_ordertime[1]);
                                intent.setClass(tijiao_Order.this,queren_Order.class);
                                startActivityForResult(intent,3);
                                tijiao_Order.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    break;

                case R.id.addaddress:
                    intent=new Intent();
                    intent.setClass(tijiao_Order.this,add_address.class);
                    startActivityForResult(intent,0);
                    tijiao_Order.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;

                case R.id.addtime:
                    onDoublePicker();
                    break;

                case R.id.addyouhuijuan:
                    intent=new Intent();
                    intent.setClass(tijiao_Order.this,youhuijuan_activity.class);
                    startActivityForResult(intent,1);
                    tijiao_Order.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode)
        {
            case 0:

                //Toast.makeText(tijiao_Order.this,item.name.toString()+item.sex.toString()+item.telphone.toString()+item.School.toString()+item.address.toString(),Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Address item= (Address) data.getSerializableExtra("address");

                    School=item.getSchool();
                    Address=item.getAddress();
                    name.setText(item.getName());
                    sex.setText(item.getSex());
                    tel.setText(item.getDizhi_telphonenum());
                    address.setText(item.getSchool()+item.getAddress());
                    address_hint.setVisibility(View.INVISIBLE);
                    name.setVisibility(View.VISIBLE);
                    sex.setVisibility(View.VISIBLE);
                    tel.setVisibility(View.VISIBLE);
                    address.setVisibility(View.VISIBLE);
                break;
            case 2:
                Youhuijuan value= (Youhuijuan) data.getSerializableExtra("youhuijuan");
                int modle_cost=cost;
                if(modle_cost>=Integer.parseInt(value.getCoupon_limit().toString()))
                {
                    youhuijuan.setText(value.getCoupon_name());
                    if(value.getCoupon_type().equals("1"))
                    {
                        modle_cost+=(int)Double.parseDouble(value.getCoupon_rebate().toString());
                        zongjia_price=modle_cost+10;
                        zongjia.setText(zongjia_price+"元");
                        coupon_id=value.getCoupon_id();
                    }
                    if(value.getCoupon_type().equals("2"))
                    {
                        modle_cost*=Double.parseDouble(value.getCoupon_rebate().toString());
                        zongjia_price=modle_cost+10;
                        zongjia.setText(String.valueOf(zongjia_price)+"元");
                        coupon_id=value.getCoupon_id();
                    }
                }
                else
                {
                    ToastUtil.shortToast(tijiao_Order.this,"优惠券不可用");
                }
                break;
            case 3:
                tijiao_Order.this.setResult(3);
                tijiao_Order.this.finish();
                tijiao_Order.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                break;
            default:
                break;
        }
    }

    public void onDoublePicker()
    {
        DateUtils dateutile=new DateUtils();

        final ArrayList<String> firstData = new ArrayList<>(); //数


        int mouths=dateutile.getMonth();
        int days=dateutile.getDays(dateutile.getYear(),dateutile.getMonth());

        for(int i=1;i<=days;i++)
        {
            firstData.add(mouths+"-"+i);
        }


        final ArrayList<String> secondData = new ArrayList<>();
        secondData.add("16:00-17:00");
        secondData.add("17:00-18:00");
        secondData.add("18:00-19:00");
        secondData.add("19:00-20:00");
        final DoublePicker picker = new DoublePicker(this, firstData, secondData);
        picker.setDividerVisible(true); //分割线是否显示
        picker.setCycleDisable(false);
        picker.setPadding(15);
        picker.setSelectedIndex(0, 0);
        picker.setCancelText("取消");
        picker.setCancelTextSize(18);
        picker.setSubmitText("确定");
        picker.setSubmitTextSize(18);
        //picker.setFirstLabel("于", null);
        //picker.setSecondLabel("骑/乘", "出发");
        picker.setTextSize(18);
        picker.setOnPickListener(new DoublePicker.OnPickListener() {
            @Override
            public void onPicked(int selectedFirstIndex, int selectedSecondIndex) {
                addtime_text.setText(firstData.get(selectedFirstIndex) + " " + secondData.get(selectedSecondIndex));
            }
        });
        picker.show();
    }


//按返回键 窗口向左滑出
@Override
protected void onPause()
{
    super.onPause();
    tijiao_Order.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
}
}
